﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LW.DB;
using System.Data.SqlClient;
using System.Data;

namespace iLangl.LanguageWire.DAL.LanguageWireDAL.RealStorage
{
	public class SyncrhonizationDAL : BaseStorageLWAPI, ISynchronizationDAL
	{
		public void InsertJobToSync(int jobId)
		{
			DBProvider dbProvider = new DBProvider();

			SqlParameter[] parameters = new SqlParameter[]
				{
					new SqlParameter("@JobId", jobId),
				};

			String sqlCommand =
@"
IF EXISTS(SELECT 1 FROM CustomerJobsToSync WHERE JobId = @JobId)
	RETURN

INSERT INTO [dbo].[CustomerJobsToSync]
           ([Id]
           ,[JobId]
           ,[UpdateFlag])
     VALUES
           (NEWID(),
           ,@JobId
           ,0)
";

			dbProvider.ExecuteNonQuery(sqlCommand, CommandType.Text, parameters);
		}

		public void RemoveJobFromSync(int jobId)
		{

			DBProvider dbProvider = new DBProvider();

			SqlParameter[] parameters = new SqlParameter[]
				{
					new SqlParameter("@JobId", jobId),
				};

			String sqlCommand =
@"DELETE FROM [dbo].[CustomerJobsToSync]
WHERE JobId =  @JobId
";

			dbProvider.ExecuteNonQuery(sqlCommand, CommandType.Text, parameters);

		}

		public SyncedDataLW GetJobsToSync()
		{
			SyncedDataLW finalresult = new SyncedDataLW();
			IList<Int32> result = new List<Int32>();

			byte[] timestamp = GetLastSyncedTimeStampForAssginment();


			DBProvider dbProvider = new DBProvider();
			
			SqlParameter[] parameters = new SqlParameter[]
				{
					new SqlParameter("@TimeStamp", timestamp),
				};

			String sqlCommand =
@"SELECT  * From CustomerJobsToSync
WHERE 
TimeStamp > isnull(@TimeStamp, TimeStamp)";

			using (SqlDataReader reader = dbProvider.ExecuteReader(sqlCommand, CommandType.Text, parameters))
			{
				while (reader.Read())
				{
					result.Add((Int32)reader["JobId"]);
				}
			}
			finalresult.JobsToSync = result.ToArray();
			finalresult.TimeStamp = timestamp;
			return finalresult;
		}
	
		public void MarkJobsAsSynced(SyncedDataLW synceddata)
		{
			byte[] timestamp = (byte[])synceddata.TimeStamp;

			DBProvider dbProvider = new DBProvider();

			SqlParameter[] parameters = new SqlParameter[]
				{
					new SqlParameter("@ObjectType", AssignmentEntity),
					new SqlParameter("@CurrentTimeStamp", timestamp)
				};

			String sqlCommand =
@"Update [EntitiesToSync] Set
CurrentTimeStamp = @CurrentTimeStamp
Where ObjectType = @ObjectType";

			dbProvider.ExecuteNonQuery(sqlCommand, CommandType.Text, parameters);
		}

		public const string AssignmentEntity = "assignment";

		public byte[] GetLastSyncedTimeStampForAssginment()
		{
			return GetLastSyncedTimeStamp(AssignmentEntity);
		}

		public byte[] GetLastSyncedTimeStamp(string entitydefinition)
		{
			DBProvider dbProvider = new DBProvider();

			SqlParameter[] parameters = new SqlParameter[]
				{
					new SqlParameter("@ObjectType", entitydefinition),
				};

			String sqlCommand =
@"Select CurrentTimeStamp  from [EntitiesToSync]
Where ObjectType = @ObjectType";

			object result = dbProvider.ExecuteScalar(sqlCommand, CommandType.Text, parameters);

			if (result == null)
				return null;

			return (byte[])result;
			
		}

		
	}
}
